package com.mnsq.subject.service.impl;

import com.mnsq.common.bean.conf.ThreadLocalUserId;
import com.mnsq.common.bean.entity.friends.Focus;
import com.mnsq.common.bean.entity.subject.UserDynamic;
import com.mnsq.common.dao.FocusRepository;
import com.mnsq.common.dao.UserDynamicRepository;
import com.mnsq.subject.bean.condition.DynamicCondition;
import com.mnsq.subject.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 周俭
 */
@Service
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    private FocusRepository focusRepository;

    @Autowired
    private UserDynamicRepository userDynamicRepository;

    @Override
    public List<UserDynamic> queryFocusPage(DynamicCondition dc) {
        List<Focus> myFocus = focusRepository.findBySelfId(ThreadLocalUserId.CURRENT_USER_ID.get());
        if (CollectionUtils.isEmpty(myFocus)) {
            return Collections.EMPTY_LIST;
        }
        List<Long> ids = myFocus.stream().map(Focus::getSelfId).collect(Collectors.toList());
        return userDynamicRepository.findByCreateUserIdIn(ids, PageRequest.of(dc.getPageNum(), dc.getPageSize(), Sort.by("createTime").descending()));
    }
}
